﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AdminCMS_Admin_Login : System.Web.UI.Page
{
    const string UserName = "UserName"; //记录用户名的Cookies名称

    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
        	//显示用户名
            txt_UserName.Text = AuthRole.LastLoginName;
            
            //显示样式风格
            ddl_style.SelectedValue = UIMethod.GetStyleValue();
            
            string InitKey = string.Empty;
            UIMethod.GetQuery(ref InitKey, "initkey", true);

            if (InitKey != string.Empty)
            {
                string initmd5 = UIMethod.GetMD5( UIMethod.GetAppSettings("InitKey") );
                if (InitKey == initmd5 )
                {
                    DataEntity.tab_Admin t = new DataEntity.tab_Admin();
                    t.adm_RoleType = "admin";
                    t.adm_RealName = "初始化管理员";
                    t.adm_Question = "你的生日是几号？";
                    t.adm_LoginUser = "admin";
                    t.adm_LoginPass = UIMethod.GetMD5("admin888");
                    t.adm_Email = "chenliang-ggg@163.com";
                    t.adm_Answer = "19840103";
                    t.Insert();

                    UIMethod.Alert("初始化成功");
                    UIMethod.GoToURL(Request.Path, true);                    
                }                
            }
            
        }
    }

    protected void btn_login_Click(object sender, EventArgs e)
    {
        string user = txt_UserName.Text.Trim();
        string pass = UIMethod.GetMD5(txt_PassWord.Text.Trim());

        if (!AuthRole.Login(user, pass))
        {
            UIMethod.Alert("用户或密码错误"); return;
        }
        else 
        {
            //记录样式索引
            Response.Cookies[UIMethod.UI_StyleName].Value = ddl_style.SelectedValue;
            Response.Cookies[UIMethod.UI_StyleName].Expires = DateTime.Now.AddYears(1);

            AuthRole.Redirect();
        }
    }
}
